package com.chat.common.chat.service.strategy.msg;

import com.chat.common.chat.dao.MessageDao;
import com.chat.common.chat.domain.dto.ChatMsgRecallDTO;
import com.chat.common.chat.domain.entity.Message;
import com.chat.common.chat.domain.entity.msg.MessageExtra;
import com.chat.common.chat.domain.entity.msg.MsgRecall;
import com.chat.common.chat.domain.enums.MessageTypeEnum;
import com.chat.common.common.event.MessageRecallEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

@Component
public class RecallMsgHandler extends AbstractMsgHandler<Object> {
    @Resource
    private MessageDao messageDao;
    @Resource
    private ApplicationEventPublisher applicationEventPublisher;
    @Override
    MessageTypeEnum getMsgTypeEnum() {
        return MessageTypeEnum.RECALL;
    }

    @Override
    protected void saveMsg(Message message, Object body) {
        throw new UnsupportedOperationException();
    }

    @Override
    public Object showMsg(Message msg) {
        return null;
    }

    @Override
    public Object showReplyMsg(Message msg) {
        return "撤回了一条消息";
    }

    @Override
    public String showContactMsg(Message msg) {
        return "撤回了一条消息";
    }

    public void recall(Long recallUid, Message message) {
        MessageExtra extra = message.getExtra();
        extra.setRecall(new MsgRecall(recallUid, new Date()));
        Message update = new Message();
        update.setId(message.getId());
        update.setType(MessageTypeEnum.RECALL.getType());
        update.setExtra(extra);
        messageDao.updateById(update);
        applicationEventPublisher.publishEvent(new MessageRecallEvent(this,new ChatMsgRecallDTO(message.getId(), message.getRoomId(), recallUid)));
    }
}
